package com.babel.util.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 临时计数器，缺点：重启服务器，计数器从1开始
 * @author chj
 *
 */
public class ProductCodeUtile {

	private static ProductCodeUtile productCodeUtile=null;
	
	private Date lastDate;
	private AtomicInteger number = new AtomicInteger(1);
	private int code = 0;
	
	private ProductCodeUtile (){}
	
	public static String getCode(){
		if(productCodeUtile==null){
			productCodeUtile = new ProductCodeUtile();
			productCodeUtile.lastDate=new Date();
		}else{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			if(!sdf.format(productCodeUtile.lastDate).equals(sdf.format(new Date()))){
				productCodeUtile.number = new AtomicInteger(1);
			}
		}
		productCodeUtile.code = productCodeUtile.number.getAndIncrement();
		return new DecimalFormat("000").format(productCodeUtile.code);
	}

	public Date getLastDate() {
		return lastDate;
	}

	public AtomicInteger getNumber() {
		return number;
	}

	public int getCode(int code) {
		return code;
	}
	
	
}
